//
//  DrugSeDetailViewController.m
//  安好
//
//  Created by 冯凯 on 14-12-4.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DrugSeDetailViewController.h"
#import "DrugBaseDetailViewController.h"

@interface DrugSeDetailViewController ()
{
    FKSearchBar * illSeachBar;
}
@end

@implementation DrugSeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.encyArray = [[NSMutableArray alloc]init];
    self.catPage = 1;
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
}
- (void)loadData
{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
   
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:[NSString stringWithFormat:@"%d",self.catPage] forKey:@"page"];
    [dict setObject:self.catid forKey:@"pid"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@wiki/drugCategory",kBaseURL] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        [self hiddenActionView];
        if ([statusString isEqualToString:@"0"]) {
            [self.tableView footerEndRefreshing];
        }else{
        if ([self.encyArray count] != 0){
            [self.encyArray removeAllObjects];
        }
            
        NSArray *jsonDictArr = [[responseObject objectForKey:@"data"] isEqual:[NSNull null]]?nil:[responseObject objectForKey:@"data"];
        if (jsonDictArr!=nil) {
        self.catPage++;
        for (NSDictionary * dic in jsonDictArr){
        [self.encyArray addObject:dic];
        }
        [self.tableView reloadData];
        }
     }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
    
}
#pragma mark - 导航条的布局
-(void)makeNavBar
{
    self.navigationItem.title = @"药品";
    //贴 返回 按钮的图片
    UIBarButtonItem *backImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backImg;
}
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 创建tableView 设置table的代理、添加头视图横线
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView addFooterWithTarget:self action:@selector(loadData)];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:self.tableView];
}
#pragma mark - tableView的代理方法
/**
 *  设置 各行cell高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth == 414){
        return 56;

    }else{
        
        return 46;
    }
}
/**
 *  设置 头视图的行高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.encyArray.count;
}
#pragma mark --cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"myCell";
    
    
    //  检测、查询是否有闲置的单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.textLabel.font = [UIFont systemFontOfSize:TITLE_TEXTLABEL_FOUNT_SIZE];
    cell.textLabel.textColor = [UIColor colorWithRed:31.0/255 green:33.0/255 blue:36.0/255 alpha:1];
    cell.textLabel.text = [[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"catname"];
    
    UIImageView * upImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 45, kScreenWidth - 15, 0.5)];
    if (kScreenWidth == 414){
        upImageView.frame = CGRectMake(20, 55.5, kScreenWidth - 20, 0.5);
    }else{
        upImageView.frame = CGRectMake(15, 45, kScreenWidth - 15, 0.5);

    }
    upImageView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"#c8c8c8"];
    [cell addSubview:upImageView];
    
    
    //  返回每行的单元格对象
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrugBaseDetailViewController *encyDrugMedicineVC = [[DrugBaseDetailViewController alloc]init];
    encyDrugMedicineVC.catid = TextString([[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"catid"]);
    encyDrugMedicineVC.catname = TextString([[self.encyArray objectAtIndex:indexPath.row] objectForKey:@"catname"]);
    [self.navigationController pushViewController:encyDrugMedicineVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
